<template>
	<view>
		<image src="../../static/logo-2.png" mode="widthFix" class="logo"></image>
		<view class="register-container">
			<input type="text" placeholder="输入你的邀请码" class="register-code" maxlength="6" v-model="registerCode" />
			<view class="register-desc">管理员创建员工证账号之后，你可以从你的个人邮箱中获得注册邀请码</view>
			<button class="register-btn"  open-type="getUserInfo" @tap="register()">执行注册</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				registerCode:""
			}
		},
		methods: {
			register:function(){
				let that=this
				let code ="";
				if(that.registerCode==null||that.registerCode.length==0){
					uni.showToast({
						icon:"none",
						title:"邀请码不能为空"
					})
					return
				}
				else if(/^[0-9]{6}$/.test(that.registerCode)==false){
					uni.showToast({
						icon:"none",
						title:"邀请码必须是6位数字"
					})
					return
				}
				uni.login({
					provider: 'weixin',
					success: function(resp){
						that.code = resp.code;
						console.log("code:",code);
					}
				});
				if(that.code!=""){
					uni.getUserProfile({
						desc: '用于完善用户信息',
						success: function(resp) {
							let nickName = resp.userInfo.nickName;
							let avatarUrl = resp.userInfo.avatarUrl;
							console.log(nickName);
							console.log(avatarUrl);
							console.log("code:",that.code);
							let data = {
								code: that.code,
								nickname:nickName,
								photo:avatarUrl,
								registerCode:that.registerCode
							};
							that.ajax(that.url.register,'POST',data,function(resp){
								let permission = resp.data.permission;
								uni.setStorageSync("permission",permission);
								
								 // 显示注册成功的弹窗,一个勾
								uni.showToast({
								    title: '注册成功',
								    icon: 'success',
								    duration: 2000
								})
								//跳转到index页面
								uni.switchTab({
									url:"../index/index"
								})
							});
							
						}
					});
				}else{
					uni.showToast({
						icon:"none",
						title:"获取临时授权码失败"
					})
				}
			}
		}
	}
</script>

<style lang="less">
	@import url('register.less');
</style>